// DeviceServiceImpl.java
package com.ny.service.impl;

import com.ny.entity.Device;
import com.ny.repository.DeviceRepository;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceRepository deviceRepository;

    @Override
    public List<Device> findAllDevices() {
        return deviceRepository.findAll();
    }

    @Override
    public Device findDeviceByName(String deviceName) {
        return deviceRepository.findByDeviceName(deviceName).orElse(null);
    }

    @Override
    public List<Device> findDevicesByName(String deviceName) {
        return deviceRepository.findByDeviceNameContaining(deviceName);
    }

    @Override
    public List<Device> findOfflineDevicesByTime(Date startTime, Date endTime) {
        // 将 Date 转换为 LocalDateTime
        LocalDateTime start = startTime.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime end = endTime.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDateTime();
        return deviceRepository.findByCreatedTimeBetweenAndStatus(start, end, 0); // 假设离线状态为0
    }

    @Override
    public List<Device> findOfflineDevicesByTime(LocalDateTime startTime, LocalDateTime endTime) {
        return null;
    }

   // DeviceServiceImpl.java
@Override
public List<Device> findDevicesByCreatedTimeAfter(Date createdTime) {
    return deviceRepository.findByCreatedTimeAfter(createdTime);
}



    
    @Override
    public boolean deleteDeviceById(Integer deviceId) {
        deviceRepository.deleteByDeviceId(deviceId);
        return true;
    }

    // DeviceServiceImpl.java


}
